package cn.ywang.concurrent.two;

/**
 * @author BeatificWang
 * @date 2019/7/21 17:42
 */

public class SynchronizedDemo1 implements Runnable {
	int x = 100;

	public synchronized void m1() {
		this.x = 1000;
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("x=" + this.x);
	}

	public synchronized void m2() {
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		this.x = 2000;
	}

	public static void main(String[] args) throws InterruptedException {
		SynchronizedDemo1 sd = new SynchronizedDemo1();
		new Thread(() -> sd.m1()).start();
		new Thread(() -> sd.m2()).start();
		sd.m2();
		System.out.println("Main x=" + sd.x);
	}

	@Override
	public void run() {
		this.m1();
	}
}
